package com.itheima.edy.info.manager.controller;

import com.itheima.edy.info.manager.domain.Teacher;
import com.itheima.edy.info.manager.server.TeacherServer;

import java.util.Scanner;

public abstract class BaseTeacherController {
    TeacherServer teacherServer = new TeacherServer();
    private Scanner sc = new Scanner(System.in);
    public final void start() {
        TeacherEnd:while (true){
            System.out.println("--------欢迎来到 <教师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加教师  2.删除教师  3.修改教师  4.查看教师  5.退出");
            String choose = sc.next();
            switch (choose){
                case "1":
                    //System.out.println("添加教师");
                    addTeacherInfo();
                    break;
                case "2":
                    //System.out.println("删除教师");
                    deleteTeacher();
                    break;
                case "3":
                    //System.out.println("修改教师");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看教师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用教师管理系统，再你妈的见！");
                    //System.exit(0);
                    break TeacherEnd;
                default:
                    System.out.println("您输入的有误，请重新输！！！");
                    break;

            }
        }
    }

    private final void updateTeacher() {
        String updateId = inputTeacherId();
        Teacher newTeach = inputeTeacherInfo(updateId);

        TeacherServer.updateTeacher(updateId,newTeach);
        System.out.println("修改成功");

    }

    public final void deleteTeacher() {
        String delId = inputTeacherId();
        //调用业务员中的方法根据id删除教师
        TeacherServer.deleteTeacherById(delId);
        System.out.println("删除成功!");
    }

    public final void addTeacherInfo() {
            String id;
            while (true){
            System.out.println("请输入教师id：");
            //从键盘录入教师信息
             id = sc.next();
            //判断输入的学号是否存在 使用TeacherServer中的isExists方法
            boolean flag = TeacherServer.isExists(id);
            if(flag){
                System.out.println("您输入的学号已经存在，请重新输入");
            }else {
                break;
            }
    }
        //将教师对象传进TeacherServer中的addTecacherInfo中
        Teacher teacher = inputeTeacherInfo(id);
        //根据返回的boolean类型结果，在控制台打印true/false
        boolean result = TeacherServer.addTeacherInfo(teacher);
        if(result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }

    }

    public  final void findAllTeacher(){
        //调用业务员中的获取方法，得到教师数组
        Teacher[] teachers = TeacherServer.findAllTeacher();
        //判断数组的内存地址，是否为null
        if (teachers == null) {
            System.out.println("查无信息，请添加后重试");
            return;
        }
        //遍历数组，获取教师信息并打印在控制台
        System.out.println("工号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null) {
                System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());
            }
        }
    }
    public String inputTeacherId() {
        String id;
        while (true) {
            //键盘录入要删除的教师id
            System.out.println("请输入教师的id");
            id = sc.next();
            //判断id在容器中是否存在，如果不存在，则需要一直录入
            boolean exists = teacherServer.isExists(id);
            if (!exists) {
                System.out.println("您输入的id不存在，请重新输入");
            } else {
                break;
            }
        }
        return id;
    }


    public abstract Teacher inputeTeacherInfo(String id) ;
}
